package LRU;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by luoqifei on 2017/8/22.
 */
public class LruLinkedHashMap<K,V> extends LinkedHashMap<K,V> {
    private int capacity;
   LruLinkedHashMap(int capacity){
        super(16,0.75f,true);
       this.capacity = capacity;
    }
    @Override
    public boolean removeEldestEntry(Map.Entry<K,V> eldest) {
        return size()>capacity;
    }

    public static void main(String[] args) {
        LruLinkedHashMap<String,String> lruLinkedHashMap = new  LruLinkedHashMap<String,String>(4);
        lruLinkedHashMap.put("1","1");
        lruLinkedHashMap.put("2","2");
        lruLinkedHashMap.put("3","3");
        lruLinkedHashMap.put("4","4");
        lruLinkedHashMap.get("1");
        lruLinkedHashMap.put("555","6");
        lruLinkedHashMap.put("tt","d");
        for(Iterator<Map.Entry<String,String>> it = lruLinkedHashMap.entrySet().iterator();it.hasNext();){
            System.out.println(it.next().getKey());
        }
    }
}
